<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Session;
use App\Services\MemberMsgService;

class MessageController extends Controller{
    public function list($mid){
        if(!$mid){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $message = new MemberMsgService;
            $msg_list = $message->getMemberMsgList($mid);
            $_data = array('code'=>'2x000001','msg_list'=>$msg_list);
        }
        return response()->json($_data);
    }

    /**
     * 获取实时获取订单物流 什么时候打开的..就什么时候返回数据
     */
    public function allTimeOrder($com,$order,$to){
        $post_data = array();
        $post_data["customer"] = env('EXPRESS_ID');//平台的客户编码
        $key= env('EXPRESS_KEY') ;//平台key
        $post_data["param"] = '{"com":"'.$com.'","num":"'.$order.'","to":"'.$to.'" }';
        $url='http://poll.kuaidi100.com/poll/query.do';
        $post_data["sign"] = md5($post_data["param"].$key.$post_data["customer"]);
        $post_data["sign"] = strtoupper($post_data["sign"]);
        $o="";
        foreach ($post_data as $k=>$v)
        {
            $o.= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
        }
        $post_data=substr($o,0,-1);

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );//地址
        curl_setopt ( $ch, CURLOPT_POST, 1 );//请求方式为post
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );//不打印header信息
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );//返回结果转成字符串
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );//post传输的数据。
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        return json_decode($return);
    }

    public function view($id,$company){
        $info = $this->allTimeOrder($company,$id,'');
        $_data = array('code'=>'2x000001','info'=>$info);
        return response()->json($_data);
    }

    public function updatemsg(){

        $message = new MemberMsgService;

        $msg_id = intval($_POST['message_id']);
        $data['is_read'] = 'yes';

        $res = $message->updateMsg($msg_id,$data);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'消息已读取');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'读取消息错误');
        }

        return response()->json($_data);
    }
}
